<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="承运商" prop="carrierName">
    <el-select v-model="queryParams.carrierId" placeholder="请选择承运商" clearable >
                <el-option
                  v-for="dict in carrierList"
                  :key="dict.carrierNo"
                  :label="dict.carrierName"
                  :value="dict.carrierNo"
                   @keyup.enter="handleQuery"
                ></el-option>
              </el-select>
      </el-form-item>
      <el-form-item label="发货日期" prop="deliveryDate">
        <el-date-picker clearable
          v-model="queryParams.deliveryDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择发货日期">
        </el-date-picker>
        <!-- <el-date-picker clearable v-model="queryParams.deliveryDate" type="datetime" placeholder="请选择有效期"
          value-format="YYYY-MM-DD HH:mm:ss" /> -->
      </el-form-item>
      <el-form-item label="快递单号" prop="trackingNo">
        <el-input v-model="queryParams.trackingNo" placeholder="请输入快递单号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="deliveryList" @selection-change="handleSelectionChange" :border="true">
      <el-table type="selection" width="55" align="center" />
      <el-table-column label="出库单主表Id" align="center" prop="shipmentOrderId">
        <template #default="scope">
          <router-link :to="'/wms/orderdetail/index?shipmentOrderId=' + scope.row.shipmentOrderId" class="link-type">
            <span>查看出库单</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="承运商" align="center" prop="carrierName" />
      <el-table-column label="发货日期" align="center" prop="deliveryDate" width="180"/>
        <!-- <template #default="scope">
          <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="快递单号" align="center" prop="trackingNo">
        <template #default="scope">
          <a :href="'https://www.kuaidi100.com/all/hhair56.shtml?mscomnu='+scope.row.trackingNo" class="link-type">{{ scope.row.trackingNo }}</a>
          </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:delivery:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:delivery:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改发货记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="deliveryRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出库单主表Id" prop="shipmentOrderId">
          <el-input v-model="form.shipmentOrderId" placeholder="请输入出库单主表Id" />
        </el-form-item>
        <el-form-item label="承运商Id" prop="carrierId">
          <el-input v-model="form.carrierId" placeholder="请输入承运商Id" />
        </el-form-item>
        <el-form-item label="发货日期" prop="deliveryDate">
          <el-date-picker clearable v-model="form.deliveryDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择发货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="快递单号" prop="trackingNo">
          <el-input v-model="form.trackingNo" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="删除标识" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标识" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Delivery">
import { listDelivery, getDelivery, delDelivery, addDelivery, updateDelivery } from "@/api/wms/delivery";
import { listCarrier } from "@/api/wms/carrier";

import Index from "../order/index.vue";

const { proxy } = getCurrentInstance();

const deliveryList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 承运商
const carrierList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    shipmentOrderId: null,
    carrierId: null,
    deliveryDate: null,
    trackingNo: null,
    supplierName: null,
    carrierName:null,
  },
  rules: {
    delFlag: [
      { required: true, message: "删除标识不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询发货记录列表 */
function getList() {
  loading.value = true;
  listDelivery(queryParams.value).then(response => {
    deliveryList.value = response.rows;
    console.log(deliveryList.value);

    console.log(response.rows);
    total.value = response.total;
    loading.value = false;
  });
}

grtCarrier()

/**查询承运商名称 */
function grtCarrier() {

  listCarrier(queryParams.value).then(response => {
    carrierList.value = response.rows;

    // console.log(response.rows);
    total.value = response.total;
    loading.value = false;
  });
}




// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    shipmentOrderId: null,
    carrierId: null,
    deliveryDate: null,
    trackingNo: null,
    remark: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("deliveryRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加发货记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getDelivery(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改发货记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["deliveryRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDelivery(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDelivery(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除发货记录编号为"' + _ids + '"的数据项？').then(function () {
    return delDelivery(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/delivery/export', {
    ...queryParams.value
  }, `delivery_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style scoped>
.demo-datetime-picker {
  display: flex;
  width: 100%;
  padding: 0;
  flex-wrap: wrap;
}

.demo-datetime-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}

.demo-datetime-picker .block:last-child {
  border-right: none;
}

.demo-datetime-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}
</style>
